var getlog = require("../../utils/judgelog.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeData: [{ 'index': '1', 'typeName': '数码' }, { 'index': '2', 'typeName': '办公' }, { 'index': '3', 'typeName': '男装' }, { 'index': '4', 'typeName': '女装' }, { 'index': '5', 'typeName': '美妆' }, { 'index': '6', 'typeName': '鞋子' }, { 'index': '7', 'typeName': '箱包' }, { 'index': '8', 'typeName': '书籍' }, { 'index': '9', 'typeName': '其他' },],


    activeIndex: 0,
    toViewType:'view0',
    dataArray:[],
    total:'',
    page: 1,

    searchdata: ''

  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initall();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  typeClick: function (e) {//监听分类点击
    console.log(e)
    var that = this;
    var index = 0;
    for (var i = 0; i < this.data.typeData.length; i++) {

      if (this.data.typeData[i].index === e.currentTarget.dataset.item.index) {
        index = i
        console.log(index)
        break
      }
    }

    that.setData({
      activeIndex: index,//设置当前索引
    })
    // console.log(this.data.activeIndex + '' + this.data.activeIndexType)
    if (index > 0) {
      that.setData({
        toViewType: 'view' + (index - 1)
      })
    }
    this.onloaddata();
  },
  onloaddata: function () {//数据展示
    var that = this
    that.setData({
      page: 1,
      total: 0
    })
    var api = "http://47.103.21.65:5645/sell/getSellInfoByType";
    wx.request({
      url: api,
      method: "POST",

      data: {  //参数传递
        typeId: this.data.activeIndex+ 1,
        page: 1
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

      },

      success: function (res) {
        console.log(res.data);

        console.log(res.data.data.total - 8)
        that.setData({
          total: res.data.data.total - 8,
          dataArray: res.data.data.info
        })
        console.log(res.data.info)

      },
      fail: function (error) { //请求失败
        console.log(error);

      }
    })
  },
  initall: function () {//数据展示
    var that = this
    that.setData({
      page: 1,
      total: 0
    })
    var api = "http://47.103.21.65:5645/sell/getSellInfoByType";
    wx.request({
      url: api,
      method: "POST",

      data: {  //参数传递
        typeId: this.data.activeIndex + 1,
        page: 1
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

      },

      success: function (res) {
        console.log(res.data);
        that.setData({
          total: res.data.data.total - 8,
          dataArray: res.data.data.info
        })
        console.log(res.data.info)

      },
      fail: function (error) { //请求失败
        console.log(error);

      }
    })
  },
  loadMore: function () {
    var that = this
    var pagei = this.data.page
    var array = this.data.dataArray
    if (that.data.total > 0) {
      that.setData({
        page: pagei + 1
      })
      var api = "http://47.103.21.65:5645/sell/getSellInfoByType";

     
      wx.request({
        url: api,
        method: "POST",

        data: {  //参数传递
          typeId: this.data.activeIndex + 1,
          page: this.data.page
        },
        header: { //设置请求头
          'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

        },

        success: function (res) {
          console.log(res.data.data.info)
          var list = that.data.dataArray.concat(res.data.data.info);//两个数组拼接
          console.log(list);

          that.setData({
            dataArray: list,
            total: that.data.total - 8,
          })
          console.log(res.data.info)

        },
        fail: function (error) { //请求失败
          console.log(error);

        }
      })
    } else {
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      })
    }
  },
  toDeatil: function (options) {/**页面跳转 */
    var flag = -1;
    getlog.getloginmode(function (result) {
      flag = result;

      if (flag == -1 || flag == 0) {
        /**未登录 */
        wx.showToast({
          title: '请登录后查看详细信息',
          icon: 'none'
        })
      } else {
        console.log(options.currentTarget.dataset.str);
        console.log(options);
        var thingId = JSON.stringify(options.currentTarget.dataset.str);

        wx.navigateTo({
          url: '../detailPageSell/detailPageSell?thingsId=' + thingId,
        })
      }
    })

  },
  searchInit: function (e) {
    var that = this;

    that.setData({
      searchdata: e.detail.value,
      focus: false
    })
    wx.navigateTo({
      url: '../search/search?searchdata=' + that.data.searchdata,
    })
  },
})
